#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;

namespace Newtonsoft.Utilities.IO.FileSystem
{
  public class Directory : IDirectory
  {
    private readonly IFileSystem _fileSystem;
    private readonly DirectoryInfo _directoryInfo;

    public Directory(IFileSystem fileSystem, string path)
    {
      _fileSystem = fileSystem;
      _directoryInfo = new DirectoryInfo(path);
    }

    public string Name
    {
      get { return _directoryInfo.Name; }
    }

    public string FullName
    {
      get { return _directoryInfo.FullName; }
    }

    public IList<IFile> GetFiles()
    {
      return GetFiles("*", SearchOption.TopDirectoryOnly);
    }

    public IList<IFile> GetFiles(string searchPattern, SearchOption searchOption)
    {
      var files = from f in _directoryInfo.GetFiles(searchPattern, searchOption)
                  select new File(_fileSystem, f.FullName);

      return files.Cast<IFile>().ToList();
    }

    public IList<IDirectory> GetDirectories()
    {
      return GetDirectories("*", SearchOption.TopDirectoryOnly);
    }

    public IList<IDirectory> GetDirectories(string searchPattern, SearchOption searchOption)
    {
      var directories = from d in _directoryInfo.GetDirectories(searchPattern, searchOption)
                        select new Directory(_fileSystem, d.FullName);

      return directories.Cast<IDirectory>().ToList();
    }

    public void Create()
    {
      _directoryInfo.Create();
    }

    public void Delete()
    {
      _directoryInfo.Delete(true);
    }

    public bool Exists
    {
      get
      {
        _directoryInfo.Refresh();
        return _directoryInfo.Exists;
      }
    }

    public IDirectory Parent
    {
      get
      { 
        if (_directoryInfo.Parent == null) return null;
        return _fileSystem.GetDirectory(_directoryInfo.Parent.FullName);
      }
    }

    public void CopyTo(string path)
    {
      var _destination = new DirectoryInfo(path);
      RecursiveCopy(_directoryInfo, _destination, "*.*");
    }

    public DirectorySecurity GetAccessControl()
    {
      return _directoryInfo.GetAccessControl();
    }

    public void SetAccessControl(DirectorySecurity directorySecurity)
    {
      _directoryInfo.SetAccessControl(directorySecurity);
    }

    private static void RecursiveCopy(DirectoryInfo source, DirectoryInfo destination, string fileSpec)
    {
      // create destination path if not already existing
      if (!destination.Exists) destination.Create();

      // copy all files that match the filespec
      FileInfo[] files = source.GetFiles(fileSpec);
      foreach (FileInfo file in files)
      {
        string target = Path.Combine(destination.FullName, file.Name);
        file.CopyTo(target, true);
      }

      // copy all files and subdirectories from source to destination
      foreach (DirectoryInfo subDirectory in source.GetDirectories())
      {
        var subDestination = new DirectoryInfo(Path.Combine(destination.FullName, subDirectory.Name));
        RecursiveCopy(subDirectory, subDestination, fileSpec);
      }
    }
  }
}